ccrHunter_AutoCheetah = true
ccrHunter_TankMisdirection = true
function ccrHunterFeatures()
	if ccrHunter_PetMisdirection and PlayerIsSOLO() then HF_PetMisdirection() end;
	if ccrHunter_TankMisdirection and PlayerIsInDungeon() or PlayerIsInRaid() then HF_TankMisdirection() end;
	if ccrHunter_AutoPetSpec then HF_AutoPetSpec() end;
	if ccrHunter_AutoGrowl then HF_AutoGrowl() end;
	if ccrHunter_AutoMendPet then HF_MendPet() end;
	if ccrHunter_AutoCheetah then HF_AutoCheetah() end;

	-- Redone
	if ccRS.Hunter_Scatter_Shot then Hunter_Scatter_Shot() end;
	if ccRS.Hunter_3_Silencing_Shot then Hunter_Silencing_Shot() end;
	if ccRS.Hunter_3_Readiness then Hunter_Readiness() end;
	-- Talents
		-- 15
			-- Posthaste (passive)
			-- Narrow Escape (passive)
			-- Crouching Tiger, Hidden Chimera (passive)			
		-- 30
			-- Silencing shot
			if 	ccrSpec() == 1 and ccRS.Hunter_1_Silencing_Shot
			or	ccrSpec() == 2 and ccRS.Hunter_2_Silencing_Shot
			or	ccrSpec() == 3 and ccRS.Hunter_3_Silencing_Shot
			then
				Hunter_Silencing_Shot()
			end
			-- Intimidation
			if 	ccrSpec() == 1 and ccRS.Hunter_1_Intimidation
			or	ccrSpec() == 2 and ccRS.Hunter_2_Intimidation
			or	ccrSpec() == 3 and ccRS.Hunter_3_Intimidation
			then
				Hunter_Intimidation()
			end			
		-- 45
			-- Exhilaration
			if 	ccrSpec() == 1 and ccRS.Hunter_1_Exhilaration
			or	ccrSpec() == 2 and ccRS.Hunter_2_Exhilaration
			or	ccrSpec() == 3 and ccRS.Hunter_3_Exhilaration
			then
				Hunter_Exhilaration()
			end	
		-- 60
			-- Fervor
			if 	ccrSpec() == 1 and ccRS.Hunter_1_Fervor
			or	ccrSpec() == 2 and ccRS.Hunter_2_Fervor
			or	ccrSpec() == 3 and ccRS.Hunter_3_Fervor
			then
				Hunter_Fervor()
			end
			-- Dire Beast
			if 	ccrSpec() == 1 and ccRS.Hunter_1_Dire_Beast
			or	ccrSpec() == 2 and ccRS.Hunter_2_Dire_Beast
			or	ccrSpec() == 3 and ccRS.Hunter_3_Dire_Beast
			then
				Hunter_Dire_Beast()
			end	
		-- 75
			-- A Murder of Crows
			if 	ccrSpec() == 1 and ccRS.Hunter_1_A_Murder_Of_Crows
			or	ccrSpec() == 2 and ccRS.Hunter_2_A_Murder_Of_Crows
			or	ccrSpec() == 3 and ccRS.Hunter_3_A_Murder_Of_Crows
			then
				Hunter_A_Murder_Of_Crows()
			end
			-- Lynx Rush
			if 	ccrSpec() == 1 and ccRS.Hunter_1_Lynx_Rush
			or	ccrSpec() == 2 and ccRS.Hunter_2_Lynx_Rush
			or	ccrSpec() == 3 and ccRS.Hunter_3_Lynx_Rush
			then
				Hunter_Lynx_Rush()
			end
		-- 90
			-- Glaive Toss
			if 	ccrSpec() == 1 and ccRS.Hunter_1_Glaive_Toss
			or	ccrSpec() == 2 and ccRS.Hunter_2_Glaive_Toss
			or	ccrSpec() == 3 and ccRS.Hunter_3_Glaive_Toss
			then
				Hunter_Glaive_Toss()
			end
			-- Power Shot
			if 	ccrSpec() == 1 and ccRS.Hunter_1_Powershot
			or	ccrSpec() == 2 and ccRS.Hunter_2_Powershot
			or	ccrSpec() == 3 and ccRS.Hunter_3_Powershot
			then
				Hunter_Powershot()
			end
			-- Barrage
			if 	ccrSpec() == 1 and ccRS.Hunter_1_Barrage
			or	ccrSpec() == 2 and ccRS.Hunter_2_Barrage
			or	ccrSpec() == 3 and ccRS.Hunter_3_Barrage
			then
				Hunter_Barrage()
			end
end

-- General
	function Hunter_Scatter_Shot()
		if SpellIsReady("Scatter Shot")
		and (Last_CC_Scan == nil or GetTime()-Last_CC_Scan > 1.5)
		and ccrPlayerIsInCombat()
		then
			if PlayerIsSOLO() then
				local objTable = Player:GetNearbyEnemies(20)
				for i = 1, # objTable do
					if objTable[i] ~= nil
					and not objTable[i]:IsDead()
					then
						objTable[i]:SetAsUnitID("FHUnit")
						if (ccrUnitIsCastingAtMe("FHUnit") or ccrUnitIsChannelingAtMe("FHUnit"))
						then
							if ccrPlayerIsCastingOrChanneling() then SpellStopCasting() end;
							ccrCastAttack("Scatter Shot", "FHUnit")
							break
						end
					end;
				end
			end
			if PlayerIsInGroup() then
				if ccrTargetIsCastingOrChanneling() then
					if ccrTargetIsCasting() and ccrTargetHasLeftOnCast(1) then ccrCastAttack("Scatter Shot") end
					if ccrTargetIsChanneling() then ccrCastAttack("Scatter Shot") end
				end
			end
			Last_CC_Scan = GetTime()
		end
	end
	function Hunter_Readiness()
		-- [60] Readiness

		-- [54] Rapid Fire
		-- [60] Dire Beast
		-- [75] A Murder of Crows

		if ccrPlayerLevel() <= 74 then
			if SpellCDDuration("Dire Beast") > 5
			and SpellCDDuration("Rapid Fire") > 5
			and not ccrPlayerHasBuff("Rapid Fire")
			and SpellIsReady("Readiness")
			then
				ccrCast("Readiness")
			end
		end
		if ccrPlayerLevel() >= 75 then
			if SpellCDDuration("Dire Beast") > 5
			and SpellCDDuration("Rapid Fire") > 5
			and SpellCDDuration("A Murder of Crows") > 5
			and not ccrPlayerHasBuff("Rapid Fire")
			and SpellIsReady("Readiness")
			then
				ccrCast("Readiness")
			end
		end		
	end
-- Talents
	-- 15
	-- 30
		function Hunter_Silencing_Shot()
			if SpellIsReady("Silencing Shot")
			and (Last_CC_Scan == nil or GetTime()-Last_CC_Scan > 1.5)
			and ccrPlayerIsInCombat()
			then
				if PlayerIsSOLO() then
					local objTable = Player:GetNearbyEnemies(40)
					for i = 1, # objTable do
						if objTable[i] ~= nil
						and not objTable[i]:IsDead()
						then
							objTable[i]:SetAsUnitID("FHUnit")
							if (ccrUnitIsCastingAtMe("FHUnit") or ccrUnitIsChannelingAtMe("FHUnit"))
							then
								if ccrPlayerIsCastingOrChanneling() then SpellStopCasting() end;
								ccrCastAttack("Silencing Shot", "FHUnit")
								break
							end
						end;
					end
				end
				if PlayerIsInGroup() then
					if ccrTargetIsCastingOrChanneling() then
						if ccrTargetIsCasting() and ccrTargetHasLeftOnCast(1) then ccrCastAttack("Silencing Shot") end
						if ccrTargetIsChanneling() then ccrCastAttack("Silencing Shot") end
					end
				end
				Last_CC_Scan = GetTime()
			end
		end
		function Hunter_Intimidation()
			if PlayerHasPet()
			and PetIsAlive()
			and SpellIsReady("Intimidation")
			and ccrPlayerIsInCombat()
			and not ccrTargetIsBoss()
			then
				ccrCast("Intimidation")
			end
		end
	-- 45
		function Hunter_Exhilaration()
			local vPlayerHealth
			local vPetHealth
			if ccrSpec() == 1 then 
				vPlayerHealth = Hunter_1_Exhilaration_PlayerHP
				vPetHealth = Hunter_1_Exhilaration_PetHP
			end
			if ccrSpec() == 2 then
				vPlayerHealth = Hunter_2_Exhilaration_PlayerHP 
				vPetHealth = Hunter_2_Exhilaration_PetHP
			end
			if ccrSpec() == 3 then
				vPlayerHealth = Hunter_3_Exhilaration_PlayerHP 
				vPetHealth = Hunter_3_Exhilaration_PetHP
			end
			if (vPlayerHealth ~= nil and vPetHealth ~= nil) then
				if SpellIsReady("Exhilaration")
				and (ccrPlayerHealth() <= vPlayerHealth) or (PlayerHasPet() and PetIsAlive() and (PetHealth() <= vPetHealth))
				then
					ccrCast("Exhilaration")
				end
			end
		end
	-- 60
		function Hunter_Fervor()
			local vPlayerFocus
			if ccrSpec() == 1 then vPlayerFocus = Hunter_1_Fervor_Focus end;
			if ccrSpec() == 2 then vPlayerFocus = Hunter_2_Fervor_Focus end;
			if ccrSpec() == 3 then vPlayerFocus = Hunter_3_Fervor_Focus end;
			if SpellIsReady("Fervor")
			and ccrPlayerFocus() <= vPlayerFocus
			then
				ccrCast("Fervor")
			end
		end
		function Hunter_Dire_Beast()


			if SpellCanAttack("Dire Beast") then ccrCastAttack("Dire Beast") end
		end
	-- 75
		function Hunter_A_Murder_Of_Crows()
			local vSingleTargetCount
			if ccrSpec() == 1 then vSingleTargetCount = ccRS.Hunter_1_SingleTarget end;
			if ccrSpec() == 2 then vSingleTargetCount = ccRS.Hunter_2_SingleTarget end;
			if ccrSpec() == 3 then vSingleTargetCount = ccRS.Hunter_3_SingleTarget end;

			if SpellIsReady("A Murder of Crows")
			and (ccrTargetCount(10) >= 1 and ccrTargetCount(10) <= vSingleTargetCount)
			then
				if ccrPlayerFocus() >= 60
				and SpellCanAttack("A Murder of Crows")
				then
					ccrCastAttack("A Murder of Crows")
				end
				if ccrPlayerFocus() < 60 then
					if SpellCanAttack(Hunter_Use_Steady_or_Cobra()) then
						ccrCastAttack(Hunter_Use_Steady_or_Cobra())
					end
				end
			end
		end
		function Hunter_Lynx_Rush()
			local vHostileCount
			if ccrSpec() == 1 then vHostileCount = Hunter_1_Lynx_Rush_Count end;
			if ccrSpec() == 2 then vHostileCount = Hunter_2_Lynx_Rush_Count end;
			if ccrSpec() == 3 then vHostileCount = Hunter_3_Lynx_Rush_Count end;
			if SpellIsReady("Lynx Rush")
			and PlayerHasPet()
			and PetIsAlive()
			and ccrPlayerIsInCombat()
			then
				if Pet:GetNearbyEnemies(10) >= vHostileCount then ccrCast("Lynx Rush") end
			end
		end
	-- 90
		function Hunter_Glaive_Toss()
			local vSingleTargetCount
			if ccrSpec() == 1 then vSingleTargetCount = ccRS.Hunter_1_SingleTarget end;
			if ccrSpec() == 2 then vSingleTargetCount = ccRS.Hunter_2_SingleTarget end;
			if ccrSpec() == 3 then vSingleTargetCount = ccRS.Hunter_3_SingleTarget end;

			if SpellIsReady("Glaive Toss")
			and (ccrTargetCount(10) >= 1 and ccrTargetCount(10) <= vSingleTargetCount)
			then
				if SpellCanAttack("Glaive Toss") then
					ccrCastAttack("Glaive Toss")
				end
				if ccrPlayerFocus() < 15 then
					if SpellCanAttack(Hunter_Use_Steady_or_Cobra()) then
						ccrCastAttack(Hunter_Use_Steady_or_Cobra())
					end
				end
			end
		end
		function Hunter_Powershot()
			local vSingleTargetCount
			if ccrSpec() == 1 then vSingleTargetCount = ccRS.Hunter_1_SingleTarget end;
			if ccrSpec() == 2 then vSingleTargetCount = ccRS.Hunter_2_SingleTarget end;
			if ccrSpec() == 3 then vSingleTargetCount = ccRS.Hunter_3_SingleTarget end;

			if SpellIsReady("Powershot") 
			and not ccrPlayerIsMoving()
			and (ccrTargetCount(10) >= 1 and ccrTargetCount(10) <= vSingleTargetCount)
			then
				if SpellCanAttack("Powershot") then
					ccrCastAttack("Powershot")
				end
				if ccrPlayerFocus() < 15 then
					if SpellCanAttack(Hunter_Use_Steady_or_Cobra()) then
						ccrCastAttack(Hunter_Use_Steady_or_Cobra())
					end
				end
			end
		end
		function Hunter_Barrage()
			local vSingleTargetCount
			if ccrSpec() == 1 then vSingleTargetCount = ccRS.Hunter_1_SingleTarget end;
			if ccrSpec() == 2 then vSingleTargetCount = ccRS.Hunter_2_SingleTarget end;
			if ccrSpec() == 3 then vSingleTargetCount = ccRS.Hunter_3_SingleTarget end;

			if SpellIsReady("Barrage")
			and (ccrTargetCount(10) >= 1 and ccrTargetCount(10) <= vSingleTargetCount)
			then
				if SpellCanAttack("Barrage") then
					ccrCastAttack("Barrage")
				end
				if ccrPlayerFocus() < 30 then
					if SpellCanAttack(Hunter_Use_Steady_or_Cobra()) then
						ccrCastAttack(Hunter_Use_Steady_or_Cobra())
					end
				end
			end
		end

function Hunter_Readiness()
	if SpellIsReady("Readiness")
	and not ccrPlayerHasBuff("Rapid Fire")
	and ccrPlayerIsInCombat()
	and ccrValidTarget()
	and ccrTargetIsWithinRanged()
	then
		if onCD("Rapid Fire")
		and (onCD("Fervor") or onCD("Dire Beast"))
		and (onCD("A Murder of Crows") or onCD("Blink Strikes") or onCD("Lynx Rush"))
		and (onCD("Glaive Toss") or onCD("Powershot") or onCD("Barrage"))
		then
			ccrCast("Readiness")
		end
	end
end
function Hunter_Use_Steady_or_Cobra()
	if ccrPlayerLevel() < 81 then return "Steady Shot" end
	if ccrPlayerLevel() >= 81 then
		if ccrSpec() == 1 or ccrSpec() == 3 then return "Cobra Shot" end
		if ccrSpec() == 2 then return "Steady Shot" end
	end
end
--

function HF_TankMisdirection()
	if ccRS.Hunter_Tank_Misdirection then 
		local vGroupType
		local vTank
		local objTank
		if IsInGroup() then vGroupType = "party" end;
		if IsInRaid() then vGroupType = "raid" end;
		if vGroupType == nil then return end;
		for i = 0, GetNumGroupMembers() do
			if ccrUnitIsTank(vGroupType .. tostring(i)) then vTank = vGroupType .. tostring(i) end;
		end
		if vTank ~= nil or GetNumGroupMembers() == 3 then
			objTank = GetObjectFromUnitID(vTank)
			if objTank and objTank:Exists() and not objTank:IsDead() and objTank:InLineOfSight() then
				if not onCD("Misdirection")
				and not ccrPlayerHasBuff("Misdirection")
				and not ccrUnitHasBuff(vTank, "Tricks of the Trade")
				and not ccrUnitHasBuff(vTank, "Misdirection")
				and ccrTargetName() ~= "Weak Spot"
				then
					ccrCast("Misdirection", vTank)
				end
			end
		end
	end
end

function HF_PetMisdirection()
	if ccRS.Hunter_Pet_Misdirection then
		local vThreat={UnitDetailedThreatSituation("Player","Target")}
		local objPet = GetObjectFromUnitID("Pet")
		if ccrPetIsAlive()
		and not SpellIsOnCooldown("Misdirection")
		and not ccrPlayerHasBuff("Misdirection")
		and vThreat[3]~=nil
		and vThreat[3]>=75
		and objPet and objPet:Exists() and not objPet:IsDead() and objPet:InLineOfSight()
		then
			ccrCast("Misdirection", "Pet")
		end
	end
end

function HF_AutoGrowl()
	local vState = {GetSpellAutocast("Growl", "Pet")}
	if (not PlayerIsSOLO() or PlayerIsInGroup()) then
		if vState[2] == 1 then
			DisableSpellAutocast("Growl");
		end
	else
		if vState[2] == nil then
			EnableSpellAutocast("Growl");
		end
	end
end

function HF_MendPet()
	if ccrUnitHealth("Pet") > 0
	and ccrUnitHealth("Pet") < 75
	and ccrDistance("Player", "Pet") < 48
	then
		if SpellIsReady("Mend Pet")
		and not ccrUnitHasBuff("Pet", "Mend Pet")
		then
			ccrCast("Mend Pet")
		end
	end
end

function HF_AutoPetSpec()
	if not HAPS then
		HAPS = CreateFrame("Frame")
		HAPS:SetScript("OnUpdate", HAPS_OnUpdate)
	end
end

function HAPS_OnUpdate(self, elapsed)
	if PlayerIsSOLO() 
	and (v_Last_Pet_Spec_Check == nil or GetTime()-v_Last_Pet_Spec_Check > 1)
	then
		if ccrPetSpec() ~= 2 then SetSpecialization(2, true) end;
		v_Last_Pet_Spec_Check = GetTime()
	end
	if PlayerIsInDungeon()
	or PlayerIsInRaid()
	or PlayerIsInGroup()
	and (v_Last_Pet_Spec_Check == nil or GetTime()-v_Last_Pet_Spec_Check > 1)
	then
		if ccrPetSpec() ~= 1 then SetSpecialization(1, true) end;
	end
end

function HF_AutoCheetah()
	if not HAC then
		HAC = CreateFrame("Frame")
		HAC:SetScript("OnUpdate", HAC_OnUpdate)
	end
end

function HAC_OnUpdate(self, elapsed)
	if ccrHunter_AutoCheetah
	and ccrPlayerIsInControl()
	and not ccrPlayerHasBuff("Feign Death")
	and not ccrPlayerHasBuff("Stealth")
	and not ccrPlayerHasBuff("Camoflague")
	and not ccrPlayerIsCastingOrChanneling()
	and (v_Last_Aspect_Check == nil or GetTime()-v_Last_Aspect_Check > 1)
	then
		if (ccrPlayerIsInCombat() or IsMounted()) then
			if not ccrPlayerHasBuff("Aspect of the Hawk")
			and not ccrPlayerHasBuff("Aspect of the Iron Hawk")
			and SpellIsReady("Aspect of the Hawk")
			then
				ccrCast("Aspect of the Hawk")
			end
		else
			if not ccrPlayerHasBuff("Aspect of the Cheetah")
			and not ccrPlayerHasBuff("Aspect of the Pack")
			and SpellIsReady("Aspect of the Cheetah")
			then
				ccrCast("Aspect of the Cheetah")
			end;
		end
		v_Last_Aspect_Check = GetTime()
	end
end